package com.guigu.buy.controller;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.buy.pojo.SysRole;
import com.guigu.buy.pojo.SysUser;
import com.guigu.buy.service.ISysUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@RestController
@RequestMapping("/buy/sysUser")
@CrossOrigin
public class SysUserController {
    //自动注入service
    @Autowired
    private ISysUserService sysUserService;
    //处理登陆请求
    @RequestMapping("Login")
    private SysUser loginback(@RequestBody SysUser user){
        QueryWrapper<SysUser> dl = new QueryWrapper<SysUser>();
        dl.eq("Login_name",user.getLoginName());
        dl.eq("password",user.getPassword());
        SysUser one = sysUserService.getOne(dl);
        return one;
    }
    //获取所有后台用户请求
    @RequestMapping("/getAllUsers/{currentPage}/{pageSize}")
    private PageInfo<SysUser> getAllUser(@PathVariable int currentPage, @PathVariable int pageSize , @RequestBody SysUser sysUser){
        QueryWrapper<SysUser> qw=new QueryWrapper<>();
        if (StringUtils.hasText(sysUser.getName())){
            qw.like("name",sysUser.getName());
        }
            qw.eq("locked",0);

        Page<SysUser> page = PageHelper.startPage(currentPage,pageSize);
        List<SysUser> list = sysUserService.list(qw);
        for (SysUser user:list){
            List<SysRole> roles = sysUserService.getRoleByUserId(user);

            System.out.println("roles"+roles);
            user.setRoles( roles);
        }
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    //改变后台用户状态请求
    @RequestMapping("/changeUserstatus")
    private boolean  changeUserstatus(@RequestBody SysUser user){

        boolean b = sysUserService.updateById(user);

        return b;
    }
    //文件上传请求

    @PostMapping("/upload")
    public String upload(MultipartFile photo) {
        System.out.println(photo);

        // 图片在服务器上保存的路径：
        // "d:pictures"
        File dir = new File("D:\\img\\sysUser");
        if(!dir.exists())
            dir.mkdir();
        // 建议建目录存放图片，目录名可以是时间，可以是类型
        String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf("."));
        // 重新命名新的图片，用uuid字符串
        String filename = UUID.randomUUID() + "_" + suffix;
        try {
            // 写文件
            photo.transferTo(new File(dir, filename));
        } catch (IOException e) {
            e.printStackTrace();
        }

        String result = "http://127.0.0.1:10002/sysUser/"+filename;
        System.out.println(result);
        // 返回图片路径
        return result;
    }

    @PostMapping("/add")
    public String add(@RequestBody SysUser users) {
        // 默认正常
        users.setStatus("0");
        users.setLocked(0);
        boolean flag = sysUserService.save(users);
        if (flag){
            return "ok";
        }else {
            return "no";
        }
    }
    @RequestMapping("/getUserById/{id}")
    public SysUser getUserById(@PathVariable Integer id){
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        System.out.println("id:"+id);
        qw.eq("id",id);
        SysUser user=sysUserService.getOne(qw);

        return user;
    }
    @RequestMapping("/deleteUser/{id}")
    public Object deleteEmployee(@PathVariable Long id) {
        // 从数据库中获取员工信息
        SysUser user = sysUserService.getById(id);
        if (user != null) {
            // 将员工的状态改为1，代表删除
            user.setLocked(1);
            // 更新员工信息
            sysUserService.updateById(user);
        }
        // 返回相应的结果
        return true;
    }
    @RequestMapping("/updateUserById")
    @ResponseBody
    public boolean updateUserById(@RequestBody SysUser user){
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.eq("id",user.getId());
        boolean flag =sysUserService.update(user,qw);
        return flag;
    }

}
